## This code include scripts for Appendix Tables
# Table A2
# Table A4
# Table A3
# Table A5
# Table A6
# Table A7



rm(list = ls())
library(readr)
library(ggplot2)
library(stargazer)
library(lme4)
library(dplyr)
library(haven)
library(texreg)
library(tidyr)

load("Final_JPR/data/paperdata.RData")
data <- paperdata %>%
       filter(tek_count>0)

source("Final_JPR/Code/coefplot.R")
source("Final_JPR/Code/marginal_effects.R")
source("Final_JPR/Code/setup.R")


########################################## Table A2
## fixed-effects

## dependent variable
dv <- 'onset_do_flag' 
## controls
ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
          'ctygdppc_log', 'ctypop_log', 'factor(gwgroupid)', 'factor(year)')

ivs_1 <- c('best_tlineq_total', 'tekbest_status_excl')
ivs_2 <- c('worst_tlineq_total', 'tekworst_status_excl')
ivs_3 <- c("median_tlineq_total",'tekmedian_status_excl')
ivs_4 <- c('nearst_tlineq_total','teknearst_status_excl')

f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))

#
ivs <- c('best_tlineq_total','worst_tlineq_total','median_tlineq_total','nearst_tlineq_total')


## using log of transnational inequality
model_onset_fixed<- list()
for (i in 1:length(ivs)){
        model_onset_fixed [[i]] <- lm(eval(parse(text = paste0('f_do_', i))), 
                                      data = data)
}




screenreg(model_onset_fixed)
## dependent variable
dv <- 'incidence_flag' 


ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
          'ctygdppc_log', 'ctypop_log', 'factor(gwgroupid)', 'factor(year)')

ivs_1 <- c('best_tlineq_total', 'tekbest_status_excl')
ivs_2 <- c('worst_tlineq_total', 'tekworst_status_excl')
ivs_3 <- c("median_tlineq_total",'tekmedian_status_excl')
ivs_4 <- c('nearst_tlineq_total','teknearst_status_excl')

f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))

#
ivs <- c('best_tlineq_total','worst_tlineq_total','median_tlineq_total','nearst_tlineq_total')


## using log of transnational inequality
model_incidence_fixed<- list()
for (i in 1:length(ivs)){
        model_incidence_fixed[[i]] <- lm(eval(parse(text = paste0('f_do_', i))), 
                                      data = data)
}



modSD = cluster_se(ModelResults = c(model_onset_fixed,model_incidence_fixed), data = data, 
                   clusterid = "gwgroupid") 

# Table A2
texreg(c(model_onset_fixed,model_incidence_fixed), file = "Final_JPR/tables/appendix_tables/Table_A2.tex",
       stars = c(0.01, 0.05, 0.1),
       caption = "Robustness Check: The Fixed-Effect Regression Results for Ethnic Conflict (1992-2020)",
       caption.above = TRUE,
       use.packages = FALSE,
       label = "tab:tab2",
       scalebox = 0.7,
       override.se =  modSD,
       custom.header = list("DV: Conflict Onset" = 1:4,
                            "DV: Conflict Prevalence" = 5:8),
       custom.model.names = c("M1:Best", "M2:Worst", "M3:Median", "M4:Nearest",
                              "M5:Best", "M6:Worst", "M7:Median", "M8:Nearest"),
       custom.coef.map = list("best_tlineq_total" = "Transnational inequality",
                              "tekbest_status_excl" =  "TEK status excluded",
                              "lineq_total" = "Horizontal inequality",
                              "status_excl" =  "Status excluded",  
                              "lsize " =  "Relative group size",
                              "family_warhistdummy" =  "Previous rebellions",
                              "family_downgraded2" = "Status downgraded",
                              "ctygdppc_log" = "Ln(Country GDP per capita)",
                              "ctypop_log" = "Ln(Country population)",
                              "worst_tlineq_total" = "Transnational inequality",
                              "tekworst_status_excl" =  "TEK status excluded",
                              "median_tlineq_total" = "Transnational inequality",
                              "tekmedian_status_excl"  =  "TEK status excluded",
                              "nearst_tlineq_total" = "Transnational inequality",
                              "teknearst_status_excl" ="TEK status excluded",
                              "(Intercept)" = "Intercept"),
       custom.gof.rows = list("Group Fixed  Effects" = c("YES", "YES", "YES", "YES", "YES", "YES","YES", "YES"),
                              "Year Fixed  Effects" = c("YES", "YES", "YES", "YES", "YES", "YES","YES", "YES")),
       digits = 3)


########################################## Table A4 
## Use somple OLS


## dependent variable
dv <- 'onset_do_flag' 
## controls
ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
          'ctygdppc_log', 'ctypop_log')

ivs_1 <- c('best_tlineq_total', 'tekbest_status_excl')
ivs_2 <- c('worst_tlineq_total', 'tekworst_status_excl')
ivs_3 <- c("median_tlineq_total",'tekmedian_status_excl')
ivs_4 <- c('nearst_tlineq_total','teknearst_status_excl')

f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))

#
ivs <- c('best_tlineq_total','worst_tlineq_total','median_tlineq_total','nearst_tlineq_total')


## using log of transnational inequality
model_onset_ols<- list()
for (i in 1:length(ivs)){
        model_onset_ols[[i]] <- lm(eval(parse(text = paste0('f_do_', i))), 
                                      data = data)
}

screenreg(model_onset_ols)

## dependent variable
dv <- 'incidence_flag' 


ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
          'ctygdppc_log', 'ctypop_log')

ivs_1 <- c('best_tlineq_total', 'tekbest_status_excl')
ivs_2 <- c('worst_tlineq_total', 'tekworst_status_excl')
ivs_3 <- c("median_tlineq_total",'tekmedian_status_excl')
ivs_4 <- c('nearst_tlineq_total','teknearst_status_excl')

f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))

#
ivs <- c('best_tlineq_total','worst_tlineq_total','median_tlineq_total','nearst_tlineq_total')


## using log of transnational inequality
model_incidence_ols<- list()
for (i in 1:length(ivs)){
        model_incidence_ols[[i]] <- lm(eval(parse(text = paste0('f_do_', i))), 
                                         data = data)
}

screenreg(model_incidence_ols)
# Table A4
modSD = cluster_se(ModelResults = c(model_onset_ols,model_incidence_ols), data = data, 
                   clusterid = "gwgroupid") 
texreg(c(model_onset_ols,model_incidence_ols), file = "Final_JPR/tables/appendix_tables/Table_A4.tex",
       stars = c(0.01, 0.05, 0.1),
       caption = "Robustness Check: The Pooled OLS Regression Results for Ethnic Conflict (1992-2020)",
       caption.above = TRUE,
       use.packages = FALSE,
       override.se =  modSD,
       label = "tab:tab3",
       scalebox = 0.7,
       custom.header = list("DV: Conflict Onset" = 1:4,
                            "DV: Conflict Prevalence" = 5:8),
       custom.model.names = c("M1:Best", "M2:Worst", "M3:Median", "M4:Nearest",
                              "M5:Best", "M6:Worst", "M7:Median", "M8:Nearest"),
       custom.coef.map = list("best_tlineq_total" = "Transnational inequality",
                              "tekbest_status_excl" =  "TEK status excluded",
                              "lineq_total" = "Horizontal inequality",
                              "status_excl" =  "Status excluded",  
                              "lsize " =  "Relative group size",
                              "family_warhistdummy" =  "Previous rebellions",
                              "family_downgraded2" = "Status downgraded",
                              "ctygdppc_log" = "Ln(Country GDP per capita)",
                              "ctypop_log" = "Ln(Country population)",
                              "worst_tlineq_total" = "Transnational inequality",
                              "tekworst_status_excl" =  "TEK status excluded",
                              "median_tlineq_total" = "Transnational inequality",
                              "tekmedian_status_excl"  =  "TEK status excluded",
                              "nearst_tlineq_total" = "Transnational inequality",
                              "teknearst_status_excl" ="TEK status excluded",
                              "(Intercept)" = "Intercept"),
       custom.gof.rows = list("Group Fixed Effects" = c("No","No","No","No","No","No","No","No"),
                              "Year Fixed Effects" = c("No","No","No","No","No","No","No","No")),
       digits = 3)


########################################## Table A3

## robustness checks: relative terms with fixed effects

## dependent variable
dv <- 'onset_do_flag' 
## controls
ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
          'ctygdppc_log', 'ctypop_log','factor(gwgroupid)', 'factor(year)')

ivs_1 <- c('best_low_tlineq', 'best_high_tlineq', 'tekbest_status_excl')
ivs_2 <- c('worst_low_tlineq', 'worst_high_tlineq', 'tekworst_status_excl')
ivs_3 <- c('median_low_tlineq', 'median_high_tlineq','tekmedian_status_excl')
ivs_4 <- c('nearst_low_tlineq', 'nearst_high_tlineq','teknearst_status_excl')


f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))

#
ivs <- c('best','worst','median','nearst')


## using log of transnational inequality
model_onset_relative <- list()
for (i in 1:length(ivs)){
        model_onset_relative[[i]] <- lm(eval(parse(text = paste0('f_do_', i))), 
                                         data = data)
}


## dependent variable
dv <- 'incidence_flag' 
## controls
ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
          'ctygdppc_log', 'ctypop_log','factor(gwgroupid)', 'factor(year)')

ivs_1 <- c('best_low_tlineq', 'best_high_tlineq', 'tekbest_status_excl')
ivs_2 <- c('worst_low_tlineq', 'worst_high_tlineq', 'tekworst_status_excl')
ivs_3 <- c('median_low_tlineq', 'median_high_tlineq','tekmedian_status_excl')
ivs_4 <- c('nearst_low_tlineq', 'nearst_high_tlineq','teknearst_status_excl')


f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))

#
ivs <- c('best','worst','median','nearst')


## using log of transnational inequality
model_incidence_relative <- list()
for (i in 1:length(ivs)){
        model_incidence_relative[[i]] <- lm(eval(parse(text = paste0('f_do_', i))), 
                                             data = data)
}

# Table A3

modSD = cluster_se(ModelResults = c(model_onset_relative, model_incidence_relative), data = data, 
                   clusterid = "gwgroupid") 

texreg(c(model_onset_relative, model_incidence_relative ), file = "Final_JPR/tables/appendix_tables/Table_A3.tex",
       stars = c(0.01, 0.05, 0.1),
       override.se =  modSD,
       use.packages = FALSE,
       caption = "Robustness Check: The Fixed-Effect Regression Results of Upward and Downward Comparisons for Ethnic Conflict (1992-2020)",
       caption.above = TRUE,
       label = "tab:robust",
       scalebox = 0.7,
       custom.header = list("DV: Conflict Onset" = 1:4,
                            "DV: Conflict Prevalence" = 5:8),
       custom.model.names = c("M1:Best", "M2:Worst", "M3:Median", "M4:Nearest",
                              "M5:Best", "M6:Worst", "M7:Median", "M8:Nearest"),
       custom.coef.map = list("best_low_tlineq" = "Relative poverty",
                              "best_high_tlineq" = "Relative wealth",
                              "tekbest_status_excl" =  "TEK status excluded",
                              "lineq_total" = "Horizontal inequality",
                              "status_excl" =  "Status excluded",  
                              "lsize " =  "Relative group size",
                              "family_warhistdummy" =  "Previous rebellions",
                              "family_downgraded2" = "Status downgraded",
                              "ctygdppc_log" = "Ln(Country GDP per capita)",
                              "ctypop_log" = "Ln(Country population)",
                              "worst_low_tlineq" ="Relative poverty",
                              "worst_high_tlineq" = "Relative wealth",
                              "tekworst_status_excl" =  "TEK status excluded",
                              "median_low_tlineq" = "Relative poverty",
                              "median_high_tlineq" =  "Relative wealth",
                              "median_tlineq_total" = "Transnational inequality",
                              "tekmedian_status_excl"  =  "TEK status excluded",
                              "nearst_low_tlineq" ="Relative poverty",
                              "nearst_high_tlineq" =  "Relative wealth",
                              "teknearst_status_excl" ="TEK status excluded",
                              "(Intercept)" = "Intercept"),
       custom.gof.rows = list("Group Fixed  Effects" = c("YES", "YES", "YES", "YES", "YES", "YES","YES", "YES"),
                              "Year Fixed  Effects" = c("YES", "YES", "YES", "YES", "YES", "YES","YES", "YES")),
       digits = 3)


########################################## Table A5
################ simple OLS
dv <- 'onset_do_flag' 
## controls
ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
          'ctygdppc_log', 'ctypop_log')

ivs_1 <- c('best_low_tlineq', 'best_high_tlineq', 'tekbest_status_excl')
ivs_2 <- c('worst_low_tlineq', 'worst_high_tlineq', 'tekworst_status_excl')
ivs_3 <- c('median_low_tlineq', 'median_high_tlineq','tekmedian_status_excl')
ivs_4 <- c('nearst_low_tlineq', 'nearst_high_tlineq','teknearst_status_excl')


f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))

#
ivs <- c('best','worst','median','nearst')


## using log of transnational inequality
model_onset_relative <- list()
for (i in 1:length(ivs)){
        model_onset_relative[[i]] <- lm(eval(parse(text = paste0('f_do_', i))), 
                                        data = data)
}


## dependent variable
dv <- 'incidence_flag' 
## controls
ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
          'ctygdppc_log', 'ctypop_log')

ivs_1 <- c('best_low_tlineq', 'best_high_tlineq', 'tekbest_status_excl')
ivs_2 <- c('worst_low_tlineq', 'worst_high_tlineq', 'tekworst_status_excl')
ivs_3 <- c('median_low_tlineq', 'median_high_tlineq','tekmedian_status_excl')
ivs_4 <- c('nearst_low_tlineq', 'nearst_high_tlineq','teknearst_status_excl')


f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))

#
ivs <- c('best','worst','median','nearst')


## using log of transnational inequality
model_incidence_relative <- list()
for (i in 1:length(ivs)){
        model_incidence_relative[[i]] <- lm(eval(parse(text = paste0('f_do_', i))), 
                                            data = data)
}

# Table A5
modSD = cluster_se(ModelResults = c(model_onset_relative, model_incidence_relative), data = data, 
                   clusterid = "gwgroupid") 

texreg(c(model_onset_relative, model_incidence_relative ), file = "Final_JPR/tables/appendix_tables/Table_A5.tex",
       stars = c(0.01, 0.05, 0.1),
       override.se =  modSD,
       use.packages = FALSE,
       caption = "Robustness Check: The Pooled OLS Regression Results of Upward and Downward Comparisons for Ethnic Conflict (1992-2020)",
       caption.above = TRUE,
       label = "tab:robustols",
       scalebox = 0.7,
       custom.header = list("DV: Conflict Onset" = 1:4,
                            "DV: Conflict Prevalence" = 5:8),
       custom.model.names = c("M1:Best", "M2:Worst", "M3:Median", "M4:Nearest",
                              "M5:Best", "M6:Worst", "M7:Median", "M8:Nearest"),
       custom.coef.map = list("best_low_tlineq" = "Relative poverty",
                              "best_high_tlineq" = "Relative wealth",
                              "tekbest_status_excl" =  "TEK status excluded",
                              "lineq_total" = "Horizontal inequality",
                              "status_excl" =  "Status excluded",  
                              "lsize " =  "Relative group size",
                              "family_warhistdummy" =  "Previous rebellions",
                              "family_downgraded2" = "Status downgraded",
                              "ctygdppc_log" = "Ln(Country GDP per capita)",
                              "ctypop_log" = "Ln(Country population)",
                              "worst_low_tlineq" ="Relative poverty",
                              "worst_high_tlineq" = "Relative wealth",
                              "tekworst_status_excl" =  "TEK status excluded",
                              "median_low_tlineq" = "Relative poverty",
                              "median_high_tlineq" =  "Relative wealth",
                              "median_tlineq_total" = "Transnational inequality",
                              "tekmedian_status_excl"  =  "TEK status excluded",
                              "nearst_low_tlineq" ="Relative poverty",
                              "nearst_high_tlineq" =  "Relative wealth",
                              "teknearst_status_excl" ="TEK status excluded",
                              "(Intercept)" = "Intercept"),
       custom.gof.rows = list("Group Fixed Effects" = c("No","No","No","No","No","No","No","No"),
                              "Year Fixed Effects" = c("No","No","No","No","No","No","No","No")),
       digits = 3)




########################################## Table A6
### Use the multilve mmodels

library(lme4)

## dependent variable
dv <- 'onset_do_flag' 
## controls
ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
          'ctygdppc_log', 'ctypop_log', '(1 |year)', '(1 | countries_gwid)')

ivs_1 <- c('best_tlineq_total', 'tekbest_status_excl')
ivs_2 <- c('worst_tlineq_total', 'tekworst_status_excl')
ivs_3 <- c("median_tlineq_total",'tekmedian_status_excl')
ivs_4 <- c('nearst_tlineq_total','teknearst_status_excl')

f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))

#
ivs <- c('best_tlineq_total','worst_tlineq_total','median_tlineq_total','nearst_tlineq_total')


## using log of transnational inequality
model_onset_mlm <- list()
for (i in 1:length(ivs)){
        model_onset_mlm[[i]] <-  glmer(eval(parse(text = paste0('f_do_', i))), 
                                      data = data, family = binomial(link = "logit"))
}



## dependent variable
dv <- 'incidence_flag' 
## controls
ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
          'ctygdppc_log', 'ctypop_log', '(1 |year)', '(1 | countries_gwid)')

ivs_1 <- c('best_tlineq_total', 'tekbest_status_excl')
ivs_2 <- c('worst_tlineq_total', 'tekworst_status_excl')
ivs_3 <- c("median_tlineq_total",'tekmedian_status_excl')
ivs_4 <- c('nearst_tlineq_total','teknearst_status_excl')

f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))

#
ivs <- c('best_tlineq_total','worst_tlineq_total','median_tlineq_total','nearst_tlineq_total')


## using log of transnational inequality
model_incidence_mlm <- list()
for (i in 1:length(ivs)){
        model_incidence_mlm[[i]] <-  glmer(eval(parse(text = paste0('f_do_', i))), 
                                       data = data, family = binomial(link = "logit"))
}

# Table A6

texreg(c(model_onset_mlm,model_incidence_mlm), file = "Final_JPR/tables/appendix_tables/Table_A6.tex",
       stars = c(0.01, 0.05, 0.1),
       caption = "Robustness Check: The Multilevel Logistic Model Regression Results for Ethnic Conflict (1992-2020)",
       caption.above = TRUE,
       use.packages = FALSE,
       label = "tab:tab4",
       scalebox = 0.7,
       custom.header = list("DV: Conflict Onset" = 1:4,
                            "DV: Conflict Prevalence" = 5:8),
       custom.model.names = c("M1:Best", "M2:Worst", "M3:Median", "M4:Nearest",
                              "M5:Best", "M6:Worst", "M7:Median", "M8:Nearest"),
       custom.coef.map = list("best_tlineq_total" = "Transnational inequality",
                              "tekbest_status_excl" =  "TEK status excluded",
                              "lineq_total" = "Horizontal inequality",
                              "status_excl" =  "Status excluded",  
                              "lsize " =  "Relative group size",
                              "family_warhistdummy" =  "Previous rebellions",
                              "family_downgraded2" = "Status downgraded",
                              "ctygdppc_log" = "Ln(Country GDP per capita)",
                              "ctypop_log" = "Ln(Country population)",
                              "worst_tlineq_total" = "Transnational inequality",
                              "tekworst_status_excl" =  "TEK status excluded",
                              "median_tlineq_total" = "Transnational inequality",
                              "tekmedian_status_excl"  =  "TEK status excluded",
                              "nearst_tlineq_total" = "Transnational inequality",
                              "teknearst_status_excl" ="TEK status excluded",
                              "(Intercept)" = "Intercept"),
       custom.gof.rows = list("Group Mixed Effects" = c("YES", "YES", "YES", "YES", "YES", "YES","YES", "YES"),
                              "Year Mixed Effects" = c("YES", "YES", "YES", "YES", "YES", "YES","YES", "YES")),
       digits = 3)



########################################## Table A7
#######################################################################################################
### relative terms for MLM

## dependent variable
dv <- 'onset_do_flag' 
## controls
ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
          'ctygdppc_log', 'ctypop_log','(1 |year)', '(1 | countries_gwid)')

ivs_1 <- c('best_low_tlineq', 'best_high_tlineq', 'tekbest_status_excl')
ivs_2 <- c('worst_low_tlineq', 'worst_high_tlineq', 'tekworst_status_excl')
ivs_3 <- c('median_low_tlineq', 'median_high_tlineq','tekmedian_status_excl')
ivs_4 <- c('nearst_low_tlineq', 'nearst_high_tlineq','teknearst_status_excl')


f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))

#
ivs <- c('best','worst','median','nearst')


## using log of transnational inequality
model_onset_relative <- list()
for (i in 1:length(ivs)){
        model_onset_relative[[i]] <- glmer(eval(parse(text = paste0('f_do_', i))), 
                                         data = data, family = binomial(link = "logit"))
}


## dependent variable
dv <- 'incidence_flag' 
## controls
ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
          'ctygdppc_log', 'ctypop_log','(1 |year)', '(1 | countries_gwid)')

ivs_1 <- c('best_low_tlineq', 'best_high_tlineq', 'tekbest_status_excl')
ivs_2 <- c('worst_low_tlineq', 'worst_high_tlineq', 'tekworst_status_excl')
ivs_3 <- c('median_low_tlineq', 'median_high_tlineq','tekmedian_status_excl')
ivs_4 <- c('nearst_low_tlineq', 'nearst_high_tlineq','teknearst_status_excl')


f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))

#
ivs <- c('best','worst','median','nearst')


## using log of transnational inequality
model_incidence_relative <- list()
for (i in 1:length(ivs)){
        model_incidence_relative[[i]] <-glmer(eval(parse(text = paste0('f_do_', i))), 
                                             data = data, family = binomial(link = "logit"))
}


# Table A7
texreg(c(model_onset_relative, model_incidence_relative ), file = "Final_JPR/tables/appendix_tables/Table_A7.tex",
       stars = c(0.01, 0.05, 0.1),
       caption = "Robustness Check: The Multilevel Logistic Regression Results of Upward and Downward Comparisons for Ethnic Conflict (1992-2020)",
       caption.above = TRUE,
       use.packages = FALSE,
       label = "robust:mlm",
       scalebox = 0.7,
       custom.header = list("DV: Conflict Onset" = 1:4,
                            "DV: Conflict Prevalence" = 5:8),
       custom.model.names = c("M1:Best", "M2:Worst", "M3:Median", "M4:Nearest",
                              "M5:Best", "M6:Worst", "M7:Median", "M8:Nearest"),
       custom.coef.map = list("best_low_tlineq" = "Relative poverty",
                              "best_high_tlineq" = "Relative wealth",
                              "tekbest_status_excl" =  "TEK status excluded",
                              "lineq_total" = "Horizontal inequality",
                              "status_excl" =  "Status excluded",  
                              "lsize " =  "Relative group size",
                              "family_warhistdummy" =  "Previous rebellions",
                              "family_downgraded2" = "Status downgraded",
                              "ctygdppc_log" = "Ln(Country GDP per capita)",
                              "ctypop_log" = "Ln(Country population)",
                              "worst_low_tlineq" ="Relative poverty",
                              "worst_high_tlineq" = "Relative wealth",
                              "tekworst_status_excl" =  "TEK status excluded",
                              "median_low_tlineq" = "Relative poverty",
                              "median_high_tlineq" =  "Relative wealth",
                              "median_tlineq_total" = "Transnational inequality",
                              "tekmedian_status_excl"  =  "TEK status excluded",
                              "nearst_low_tlineq" ="Relative poverty",
                              "nearst_high_tlineq" =  "Relative wealth",
                              "teknearst_status_excl" ="TEK status excluded",
                              "(Intercept)" = "Intercept"),
       custom.gof.rows = list("Group Mixed Effects" = c("YES", "YES", "YES", "YES", "YES", "YES","YES", "YES"),
                              "Year Mixed Effects" = c("YES", "YES", "YES", "YES", "YES", "YES","YES", "YES")),
       digits = 3)



